#/bin/bash

SSH_PORT=22


if [[ -f ../config/mysql.cfg ]];then
  echo "加载DB配置文件"
  source ../config/mysql.cfg
fi

if [[ -f ../config/nacos.cfg ]];then
  echo "加载nacos配置文件"
  source ../config/nacos.cfg
fi

if [[ -f ../config/config.cfg ]];then
  echo "加载config配置文件"
  source ../config/config.cfg
fi

if [[ -n "$1" ]]; then
   NACOS_TAR_NAME=$1
fi
if [[ -n "$2" ]]; then
   NACOS_IPS=$2
fi
if [[ -n "$3" ]]; then
   MYSQL_IP=$3
fi

if [[ -n "$4" ]]; then
   MYSQL_PORT=$4
fi

if [[ -n "${workspace}" ]]; then
	NACOS_HOME=${workspace}/${NACOS_NAME}
else
  NACOS_HOME=${NACOS_NAME}
fi

NACOS_DB_URL="jdbc:mysql://${MYSQL_IP}:${MYSQL_PORT}/${NACOS_DATABASE_NAME}?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC"

path=`pwd`
nacosIps=(${NACOS_IPS//,/ })
IPS=`ip addr | grep 'state UP' -A2  | grep  "inet"  |awk '{print $2}' | cut -f1 -d'/'`

######## 启动服务 ########

function start(){
  if [[ ! -n "${nacosIps[0]}" ]]; then
    su ${USER} -c "${NACOS_HOME}/bin/nacos-stop.sh"
    sleep 3
    su ${USER} -c "${NACOS_HOME}/bin/nacos-start.sh"
  else
    ssh -p${SSH_PORT} ${nacosIps[0]} "su ${USER} -c \"${NACOS_HOME}/bin/nacos-stop.sh\""
    sleep 3
    ssh -p${SSH_PORT} ${nacosIps[0]} "su ${USER} -c \"source /etc/profile && sh ${NACOS_HOME}/bin/nacos-start.sh\""
  fi
}

######## 校验 ########
function check(){
	
	if [[ ! -n "${NACOS_HOME}" ]]; then
    echo "NACOS_HOME 未设置"
    exit
  fi
  
  if [[ ! -n ${NACOS_NAME} ]]; then
	  echo "NACOS_NAME未设置"
	  exit
	fi
	
  if [[ ! -n "${MYSQL_IP}" ]]; then
    echo "MYSQL_IP 未设置"
    exit
  fi
  
  if [[ ! -n "${MYSQL_PORT}" ]]; then
    echo "MYSQL_PORT 未设置"
    exit
  fi
  
  if [[ ! -n "${MYSQL_USERNAME}" ]]; then
    echo "MYSQL_USERNAME 未设置"
    exit
  fi
  
  if [[ ! -n "${MYSQL_PASSWORD}" ]]; then
    echo "MYSQL_PASSWORD 未设置"
    exit
  fi
  
  if [[ ! -f ../data/${NACOS_TAR_NAME} ]]; then
    echo "未找到nacos安装包"
    exit
  fi
  
  if [[ ! -n "${NACOS_IPS}" ]]; then
    echo "NACOS_IPS 未设置"
    exit
  fi
  
  if [[ ! -n "${NACOS_PORT}" ]]; then
    echo "NACOS_PORT 未设置"
    exit
  fi
	
  for i in ${!nacosIps[@]}
  do
    if [[ ! "${IPS[@]}" =~ "${nacosIps[$i]}" ]] && [[ -n ${nacosIps[$i]} ]] ;then
      ssh -p${SSH_PORT} ${nacosIps[$i]} "ls -la" >/dev/null
    fi
  done
}

######## 解压文件 ########

function xunzip(){
  
  echo "正在解压nacos安装包。。。。"
  if [[ -d ${NACOS_HOME} ]]; then
    rm -rf ${NACOS_HOME}
  fi
  mkdir -p ${NACOS_HOME}
  if [[ "../data/${NACOS_TAR_NAME}" =~ ".tar.gz" ]]; then
  	 tar -zxvf ../data/${NACOS_TAR_NAME} -C ${NACOS_HOME} --strip-components 1 >/dev/null
  else
    zipname=`unzip -l ../data/${NACOS_TAR_NAME} | awk 'NR> 3 {print $4}' |head -1`
    unzip ../data/${NACOS_TAR_NAME} -d ${NACOS_HOME} >/dev/null
    mv ${NACOS_HOME}/${zipname}* ${NACOS_HOME}
    rm -rf ${NACOS_HOME}/${zipname}
  fi
  echo "完成nacos安装包解压。。。。"
}

######## 集群启动脚本生成 ########
function startFile(){
  if [[ ${#nacosIps[*]} -ge 2 ]]; then
  	echo "#/bin/bash" >> ${NACOS_HOME}/bin/nacos-start.sh
    echo "NACOS_IPS=${NACOS_IPS}" >> ${NACOS_HOME}/bin/nacos-start.sh
    echo 'nacosIps=(${NACOS_IPS//,/ })' >> ${NACOS_HOME}/bin/nacos-start.sh
    echo 'for i in ${!nacosIps[@]}' >> ${NACOS_HOME}/bin/nacos-start.sh
    echo 'do' >> ${NACOS_HOME}/bin/nacos-start.sh
    echo "ssh -p${SSH_PORT} ${USER}@\${nacosIps[\$i]} \"${NACOS_HOME}/bin/startup.sh \"" >> ${NACOS_HOME}/bin/nacos-start.sh
    echo 'done' >> ${NACOS_HOME}/bin/nacos-start.sh
	
	  echo "#/bin/bash" >> ${NACOS_HOME}/bin/nacos-stop.sh
    echo "NACOS_IPS=${NACOS_IPS}" >> ${NACOS_HOME}/bin/nacos-stop.sh
    echo 'nacosIps=(${NACOS_IPS//,/ })' >> ${NACOS_HOME}/bin/nacos-stop.sh
    echo 'for i in ${!nacosIps[@]}' >> ${NACOS_HOME}/bin/nacos-stop.sh
    echo 'do' >> ${NACOS_HOME}/bin/nacos-stop.sh
    echo "pids=\`ssh -p ${SSH_PORT} \${nacosIps[\$i]} \"ps -ef | grep ${NACOS_HOME} | grep -v grep | grep -v bash\"| awk '{print \$2}'\` " >> ${NACOS_HOME}/bin/nacos-stop.sh
    echo -e "if [[ -n \"\${pids}\" ]]; then\n for j in \${!pids[@]}\n do\n  ssh -p ${SSH_PORT} ${USER}@\${nacosIps[\$i]} \"kill -9 \${pids[\$j]}\"\n done\nfi" >> ${NACOS_HOME}/bin/nacos-stop.sh
    echo 'done' >> ${NACOS_HOME}/bin/nacos-stop.sh
  else
    echo "#/bin/bash" >> ${NACOS_HOME}/bin/nacos-start.sh
    echo "#/bin/bash" >> ${NACOS_HOME}/bin/nacos-stop.sh
  
    echo "${NACOS_HOME}/bin/startup.sh" >> ${NACOS_HOME}/bin/nacos-start.sh
    echo "pids=\`ps -ef | grep ${NACOS_HOME} | grep -v grep | grep -v bash | awk '{print \$2}'\`" >> ${NACOS_HOME}/bin/nacos-stop.sh
    echo -e "if [[ -n \"\${pids}\" ]]; then\n for j in \${!pids[@]}\n do\n  kill -9 \${pids[\$j]}\n done\nfi" >> ${NACOS_HOME}/bin/nacos-stop.sh
  fi
  chmod +x ${NACOS_HOME}/bin/nacos-stop.sh
  chmod +x ${NACOS_HOME}/bin/nacos-start.sh
}

######## 修改配置文件 ########
function cfg(){	
  sed -i "/^server.port=/cserver.port=${NACOS_PORT}"  ${NACOS_HOME}/conf/application.properties
  sed -i "/^# spring.datasource.platform=mysql/cspring.datasource.platform=mysql"  ${NACOS_HOME}/conf/application.properties
  sed -i "/^# db.num=1/cdb.num=1"  ${NACOS_HOME}/conf/application.properties
  sed -i "/^# db.url.0=/cdb.url.0=${NACOS_DB_URL}"  ${NACOS_HOME}/conf/application.properties
  sed -i "/^# db.user.0=/cdb.user.0=${MYSQL_USERNAME}"  ${NACOS_HOME}/conf/application.properties
  sed -i "/^# db.password.0=/cdb.password.0=${MYSQL_PASSWORD}"  ${NACOS_HOME}/conf/application.properties
  
  if [[ ${#nacosIps[*]} -ge 2 ]]; then
    cp ${NACOS_HOME}/conf/cluster.conf.example ${NACOS_HOME}/conf/cluster.conf
    echo "" > ${NACOS_HOME}/conf/cluster.conf
    for i in ${!nacosIps[@]}
    do
      echo "${nacosIps[$i]}:${NACOS_PORT}" >> ${NACOS_HOME}/conf/cluster.conf
    done
    sed -i "/^    JAVA_OPT=\"\${JAVA_OPT} -server/c    JAVA_OPT=\"\${JAVA_OPT} -server -Xms${NACOS_XMS} -Xmx${NACOS_XMX} -Xmn${NACOS_XMN} -XX:MetaspaceSize=${NACOS_METASPACESIZE} -XX:MaxMetaspaceSize=${NACOS_MAXMETASPACESIZE}\""  ${NACOS_HOME}/bin/startup.sh
  else
    sed -i "/^    JAVA_OPT=\"\${JAVA_OPT} -Xms/c    JAVA_OPT=\"\${JAVA_OPT} -Xms${NACOS_XMS} -Xmx${NACOS_XMX} -Xmn${NACOS_XMN}\""  ${NACOS_HOME}/bin/startup.sh
  fi
  
  sed -i "1iCREATE DATABASE IF NOT EXISTS ${NACOS_DATABASE_NAME};"  ${NACOS_HOME}/conf/nacos-mysql.sql
  sed -i "2iUSE ${NACOS_DATABASE_NAME};"  ${NACOS_HOME}/conf/nacos-mysql.sql
}

######## 拷贝安装文件 ########
function send(){
  echo "正在拷贝nacos安装文件。。。"
  for i in ${!nacosIps[@]}
  do
    if [[ ! "${IPS[@]}" =~ "${nacosIps[$i]}" ]] && [[ -n "${nacosIps[$i]}" ]] ;then
      ssh -p${SSH_PORT} ${nacosIps[$i]} "rm -rf ${NACOS_HOME} && mkdir -p ${NACOS_HOME}"
      scp -P${SSH_PORT} -r ${NACOS_HOME} ${nacosIps[$i]}:${workspace} >> /dev/null
      ssh -p${SSH_PORT} ${nacosIps[$i]} "chown -R ${USER}:${USER} ${NACOS_HOME}"
    fi
  done
  chown -R ${USER}:${USER} ${NACOS_HOME}
  echo "完成拷贝nacos安装文件。。。"
}

######## 运行sql ########
function yunsql(){
  echo "正在运行sql文件。。。"
  remotemysqlexecute ${NACOS_HOME}/conf/nacos-mysql.sql
  echo "完成运行sql文件。。。"
}

set -e
check
xunzip
cfg
startFile
send
#yunsql
set +e
start